import QueryOptions from '@/components/QueryOptions.vue';
<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="nav">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row>
      <!-- <el-col :span="4">
        <div class="title">
          菜单结构
        </div>
        <div class="tree">
          <el-tree :data="this.$global.menuList" :props="defaultProps" @node-click="handleNodeClick"/>
        </div>
      </el-col> -->
      <el-col>
        <div class="right">
          <!-- 搜索与添加区域 -->
          <!-- Layout布局 - > 混合布局
           :gutter="20" 两个格子之间的间隔
        -->
          <!-- <el-row type="flex" justify="space-between"> -->
          <el-row :gutter="24" style="margin: 0 0 20px 0;">
            <el-col :span="4.5">
              <QueryOptions title="状态">
                <el-select v-model="queryBody.status" clearable placeholder="请选择"
                  style="min-width: 100px;max-height: 94%;">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </QueryOptions>
            </el-col>
            <el-col :span="5" class="search">
              <QueryOptions title="名称">
                <el-input placeholder="请输入菜单名称" v-model="queryBody.title">
                </el-input>
              </QueryOptions>

            </el-col>
            <el-col :span="6" class="search">
              <!-- Input输入框 - > 可清空输入框 -->
              <QueryOptions title="URL地址">
                <el-input placeholder="请输入菜单URL地址" v-model="queryBody.url" clearable>
                </el-input>
              </QueryOptions>
            </el-col>
            <e-col :span="2">
              <el-button style="color:#fff;background-color: #007d71;" icon="el-icon-search"
                @click="menuSearch"></el-button>
            </e-col>
            <el-col :span="1.8" class="top-button">
              <el-button type="primary" style="background-color: #009688;" icon="el-icon-s-platform"
                @click="addMenu">添加</el-button>
              <!-- <el-popover placement="top" trigger="click">
                <div class="dialog-info">
                  <el-button style="margin-left: 0;">启用</el-button>
                  <el-button style="margin-left: 0;">冻结</el-button>
                  <el-button style="margin-left: 0;">删除</el-button>
                </div>
                <el-button slot="reference" style="background-color: #009688; color: aliceblue;"
                  icon="el-icon-caret-bottom">操作</el-button>
              </el-popover> -->
            </el-col>
          </el-row>
          <!-- </el-row> -->
          <el-table :data="this.$global.menuList" style="width: 100%" row-key='label' border default-expand-all
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <!-- <el-table-column type="selection" width='auto' :align="align"> </el-table-column> -->
            <el-table-column prop="label" label="名称" width='auto' :align="align"></el-table-column>
            <el-table-column prop="menuURL" label="URL地址" width='auto' :align="align"></el-table-column>
            <el-table-column prop="menuPerms" label="权限标识" width='auto' :align="align">
            </el-table-column>
            <el-table-column prop="menuType" label="菜单类型" width='auto' :align="align">
              <template slot-scope="scope">
                <div>
                  {{ scope.row.menuType == 1 ? '一级菜单' : scope.row.menuType == 2 ? '二级菜单' : '不是菜单' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="menuStatus" label="状态" width='80px' :align="align">
              <template slot-scope="scope">
                <el-tag :type="scope.row.menuStatus == 1 ? 'success' : scope.row.menuStatus == 2 ? 'primary' : 'danger'"
                  disable-transitions>
                  {{ scope.row.menuStatus == 1 ? '正常' : scope.row.menuStatus == 2 ? '冻结' : '删除' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width='240px' :align="align" class="dialog">
              <template slot-scope="scope">
                <el-button v-show="scope.row.menuStatus == 1 && scope.row.childrenNum == 0 && scope.row.pStatus == 1"
                  type="danger" size="small" style="margin-left: 0;" @click="delMenu(scope.row)" plain>删除</el-button>
                <el-button v-show="scope.row.menuStatus == 3 && scope.row.childrenNum == 0 && scope.row.pStatus == 1"
                  type="success" size="small" style="margin-left: 0;" @click="activeMenu(scope.row)" plain>还原</el-button>
                <el-button v-show="scope.row.menuStatus == 1 && scope.row.childrenNum == 0 && scope.row.pStatus == 1"
                  type="primary" size="small" style="margin-left: 0;" @click="freezeMenu(scope.row)" plain>冻结</el-button>
                <el-button v-show="scope.row.menuStatus == 2 && scope.row.childrenNum == 0 && scope.row.pStatus == 1"
                  type="success" size="small" style="margin-left: 0;" @click="activeMenu(scope.row)" plain>激活</el-button>
                <el-button style="color: #fff;background-color:rgba(0, 125, 113,.5);
                  border:1px solid #007d71;margin-left: 0;" size="mini" plain
                  @click="menuEdit(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
            <el-table-column label="" width='60px' :align="align" class="dialog">
              <template slot-scope="scope">
                <el-button style="color: #fff;background-color:rgba(0, 125, 113,.5);
                  border:1px solid #007d71;margin-left: 0;" size="mini" plain @click="showMenuInfo(scope.row)">
                  <i class="el-icon-more"></i>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" current-page="1"
            page-sizes="[5, 10, 15, 20]" page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="20">
          </el-pagination> -->
          <!-- 添加菜单 -->
          <el-dialog title="添加菜单" :visible.sync="dialogMenuAddVisible" @close="menuAddClose" width="40%">
            <!-- 内容主题区域 -->
            <!-- <div> 43567890 </div> -->
            <el-form :model="menuAddInfo">

              <el-form-item label="菜单名称" :label-width="formLabelWidth">
                <el-input v-model="addMenuInfo.title" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="菜单图标" :label-width="formLabelWidth">
                <el-radio-group v-model="addMenuInfo.icon" v-for="(icon_i, key) in icons">
                  <el-radio-button :label="icon_i"> <i :class="icon_i"></i> </el-radio-button>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="菜单状态" :label-width="formLabelWidth">
                <!-- <el-input v-model="menuEditInfo[key]" autocomplete="off"></el-input> -->
                <el-select v-model="addMenuInfo.status">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="菜单地址" :label-width="formLabelWidth">
                <el-input v-model="addMenuInfo.url" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="菜单权限标识" :label-width="formLabelWidth">
                <el-input v-model="addMenuInfo.perms" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="菜单备注" :label-width="formLabelWidth">
                <el-input v-model="addMenuInfo.remark" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="父菜单ID" :label-width="formLabelWidth">
                <el-input v-model="addMenuInfo.pid" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
              <el-button @click="menuAddClose">取 消</el-button>
              <el-button type="primary" @click="menuAddEnter">确 定</el-button>
            </span>
          </el-dialog>
          <!-- 修改菜单 -->
          <el-dialog title="修改菜单" :visible.sync="dialogMenuEditVisible" @close="menuEditClose" width="40%">
            <!-- 内容主题区域 -->
            <!-- <div> 43567890 </div> -->
            <el-form :model="menuEditInfo" v-for="(value, key) in menuEditInfo">
              <el-form-item v-if="key == 'status'" :label="menuEditDict[key]" :label-width="formLabelWidth"
                v-show="value == 1 && controlInfo.childrenNum == 0 && controlInfo.pStatus == 1">
                <!-- <el-input v-model="menuEditInfo[key]" autocomplete="off"></el-input> -->
                <el-select v-model="controlInfo.status">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-else-if="key == 'icon'" :label="menuEditDict[key]" :label-width="formLabelWidth">
                <!-- <el-input v-model="menuEditInfo[key]" autocomplete="off"></el-input> -->
                <!-- <div> icon </div> -->
                <el-radio-group v-model="menuEditInfo.icon" v-for="(icon_i, key) in icons">
                  <el-radio-button :label="icon_i"> <i :class="icon_i"></i> </el-radio-button>
                </el-radio-group>
              </el-form-item>
              <el-form-item v-else-if="key == 'pid'" :label="menuEditDict[key]" :label-width="formLabelWidth"
                v-show="controlInfo.childrenNum == 0">
                <el-input v-model="menuEditInfo[key]" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item v-else-if="key != 'id'" :label="menuEditDict[key]" :label-width="formLabelWidth">
                <el-input v-model="menuEditInfo[key]" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
              <el-button @click="menuEditClose">取 消</el-button>
              <el-button type="primary" @click="menuEditEnter">确 定</el-button>
            </span>
          </el-dialog>
          <!-- 菜单详情 -->
          <el-dialog title="菜单详情" :visible.sync="dialogShowInfoVisible" @close="showMenuInfoClose" width="40%">
            <!-- 内容主题区域 -->
            <el-descriptions :column="2" border>
              <el-descriptions-item v-for="(value, key) in menuInfo" :label="menuDict[key]">
                <div v-if="key == 'menuStatus' || key == 'pStatus'">
                  {{ value == 1 ? '正常' : value == 2 ? '冻结' : '删除' }}
                </div>
                <div v-else-if="key == 'menuType'">
                  {{ value == 1 ? '一级菜单' : value == 2 ? '二级菜单' : '不是菜单' }}
                </div>
                <div v-else-if="key == 'icon'">
                  <i :class="value"></i>
                </div>
                <div v-else-if="key == 'menuRemark' && (typeof value) == 'string' && value.length > 10">
                  <el-popover placement="bottom-start" width="200" trigger="hover" :content="value">
                    <el-button slot="reference">{{ value.substring(0, 10) + "..." }}</el-button>
                  </el-popover>
                </div>
                <div v-else-if="key != 'children'">
                  {{ value }}
                </div>
                <div v-else style="display:flex;">
                  <el-tag v-for="v in value" style="margin-left:3px">
                    {{ v.menuId }}
                  </el-tag>
                </div>
              </el-descriptions-item>
            </el-descriptions>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
              <el-button @click="showMenuInfoClose">关 闭</el-button>
            </span>
          </el-dialog>
          <el-dialog title="搜索结果" :visible.sync="dialogMenuSearchVisible" @close="menuSearchClose" width="40%" fullscreen="true">
            <el-table :data="searchMenu" style="width: 100%" row-key='label' border default-expand-all
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
              <!-- <el-table-column type="selection" width='auto' :align="align"> </el-table-column> -->
              <el-table-column prop="id" label="菜单id" width='auto' :align="align"></el-table-column>
              <el-table-column prop="pid" label="父菜单id" width='auto' :align="align"></el-table-column>
              <el-table-column prop="label" label="菜单名称" width='auto' :align="align"></el-table-column>
              <el-table-column label="菜单图标" width='auto' :align="align">
                <template slot-scope="scope">
                  <i :class="scope.row.icon"></i>
                </template>
              </el-table-column>
              <el-table-column prop="url" label="菜单URL地址" width='auto' :align="align"></el-table-column>
              <el-table-column prop="perms" label="权限标识" width='auto' :align="align"></el-table-column>
              <el-table-column prop="type" label="菜单类型" width='auto' :align="align">
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.type == 1 ? '一级菜单' : scope.row.type == 2 ? '二级菜单' : '不是菜单' }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width='80px' :align="align">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.status == 1 ? 'success' : scope.row.status == 2 ? 'primary' : 'danger'"
                    disable-transitions>
                    {{ scope.row.status == 1 ? '正常' : scope.row.status == 2 ? '冻结' : '删除' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="菜单备注" width='auto' :align="align"></el-table-column>
              <el-table-column prop="createDate" label="菜单创建时间" width='auto' :align="align"></el-table-column>
              <el-table-column prop="updateDate" label="菜单修改时间" width='auto' :align="align"></el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryBody.page"
              :page-sizes="[5, 10, 15, 20]" :page-size="queryBody.pageSize"
              layout="total, sizes, prev, pager, next, jumper" :total="menuTotal">
            </el-pagination>
          </el-dialog>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import QueryOptions from '@/components/QueryOptions.vue';
export default {
  inject: ['reload'],
  data() {
    return {
      // 样式属性
      align: 'center',
      // 树属性
      // defaultProps: {
      //   children: 'children',
      //   label: 'label'
      // },
      // 状态列表
      options: [{
        value: 1,
        label: '正常'
      }, {
        value: 2,
        label: '冻结'
      }, {
        value: 3,
        label: '删除'
      }],
      // 映射字典
      menuDict: {
        'menuId': '菜单id',
        'icon': '菜单图标',
        'label': '菜单名称',
        'menuURL': '菜单地址',
        'menuType': '菜单类型',
        'menuStatus': '菜单状态',
        'menuRemark': '菜单备注',
        'menuPerms': '菜单权限标识',
        'childrenNum': '子菜单总数',
        'children': '子菜单',
        'pStatus': '父菜单状态',
        'pId': '父菜单id',
        'createDate': '菜单创建时间',
        'updateDate': '菜单更新时间'
      },
      menuEditDict: {
        title: '菜单名称',
        icon: '菜单图标',
        url: '菜单地址',
        status: '菜单状态',
        remark: '菜单备注',
        perms: '菜单权限标识',
        pid: '父菜单id'
      },
      // 图标列表
      icons: {
        radio1: 'el-icon-delete',
        radio2: 'el-icon-setting',
        radio3: 'el-icon-phone-outline',
        radio4: 'el-icon-goods',
        radio5: 'el-icon-warning-outline',
        radio6: 'el-icon-circle-plus-outline',
        radio7: 'el-icon-circle-check',
        radio8: 'el-icon-camera',
        radio8: 'el-icon-video-camera',
        radio10: 'el-icon-bell',
      },
      // 菜单状态输入值
      menuStatus: '',
      // 菜单名字输入值
      menuName: '',
      // 菜单URL地址值
      menuURL: '',
      // 修改菜单弹窗是否显示
      dialogMenuEditVisible: false,
      // 修改信息
      menuEditInfo: {},
      // 控制信息
      controlInfo: {
        status: ''
      },
      // 显示菜单信息弹窗是否显示
      dialogShowInfoVisible: false,
      // 菜单信息
      menuInfo: {},
      // 添加菜单信息弹窗是否显示
      dialogMenuAddVisible: false,
      // 添加菜单信息
      addMenuInfo: {
        icon: '',
        perms: '',
        pid: 0,
        remark: '',
        status: 1,
        title: '',
        url: ''
      },
      dialogMenuSearchVisible: false,
      // console.log(this.menuName, this.menuStatus, this.menuURL);
      queryBody: {
        page: 1,
        pageSize: 5,
        title: null,
        status: null,
        url: null
      },
      menuTotal:0,
      searchMenu: {}
    };
  },
  methods: {
    // 删除菜单
    async delMenu(row) {
      const { data: result } = await this.$http.delete("/menu", { params: { id: row.menuId } });
      if (result.success) {
        this.$message.success("菜单删除成功");
        this.reload();
      } else {
        this.$message.error("菜单删除失败");
      }
    },
    handleSizeChange(pageSize){
      this.queryBody.pageSize=pageSize;
      this.menuSearch();
    },
    handleCurrentChange(page){
      this.queryBody.page=page;
      this.menuSearch();
    },
    // 冻结菜单
    async freezeMenu(row) {
      const { data: result } = await this.$http.put("/menu/freeze", { id: row.menuId });
      if (result.success) {
        this.$message.success("菜单冻结成功");
        this.reload();
      } else {
        this.$message.error("菜单冻结失败");
      }
    },
    // 还原/激活菜单
    async activeMenu(row) {
      const { data: result } = await this.$http.put("/menu/active", { id: row.menuId });
      if (result.success) {
        this.$message.success("还原/激活菜单成功");
        this.reload();
      } else {
        this.$message.error("还原/激活菜单失败");
      }
    },
    // 搜索菜单
    async menuSearch() {
      if (this.queryBody.url == ''){
        this.queryBody.url = null;
      }
      const { data: result } = await this.$http.get("/menu/getByPageAndCondition", { params:this.queryBody});
      if (result.success) {
        this.searchMenu = result.data.records;
        this.menuTotal = result.data.total;
        this.dialogMenuSearchVisible = true;
      } else {
        this.$message.error(result.errorMsg);
      }

    },
    menuSearchClose(){
      this.dialogMenuSearchVisible = false;
    },
    // 打开添加菜单
    addMenu() {
      // console.log('open:', this.addMenuInfo);
      this.dialogMenuAddVisible = true;
    },
    // 关闭添加菜单
    menuAddClose() {
      // console.log('close:', this.addMenuInfo);
      this.dialogMenuAddVisible = false;
    },
    //确认添加菜单
    async menuAddEnter() {
      const { data: result } = await this.$http.post("/menu/", this.addMenuInfo);
      if (result.success) {
        this.$message.success("菜单添加成功");
      } else {
        this.$message.error("菜单添加失败");
      }
      this.dialogMenuAddVisible = false;
      this.reload();
    },
    // 打开编辑菜单
    menuEdit(row) {
      this.menuEditInfo = {
        id: row.menuId,
        title: row.label,
        icon: row.icon,
        url: row.menuURL,
        status: row.menuStatus,
        remark: row.menuRemark,
        perms: row.menuPerms,
        pid: row.pId
      };
      this.controlInfo = {
        childrenNum: row.childrenNum,
        pStatus: row.pStatus,
        status: row.menuStatus
      };
      this.dialogMenuEditVisible = true;
    },
    // 关闭编辑菜单弹窗
    menuEditClose() {
      // console.log(this.menuEditInfo);
      this.dialogMenuEditVisible = false;
    },
    // 确认编辑菜单弹窗
    async menuEditEnter() {
      this.menuEditInfo.status = this.controlInfo.status;
      const { data: result } = await this.$http.put("/menu/updateMenu/", this.menuEditInfo);
      if (result.success) {
        this.$message.success("菜单修改成功");
      } else {
        this.$message.error("菜单修改失败");
      }
      // console.log(this.menuEditInfo);
      this.dialogMenuEditVisible = false;
      this.reload();
    },
    // 显示菜单
    async showMenuInfo(row) {
      this.dialogShowInfoVisible = true;
      this.menuInfo = row
      // console.log(this.menuInfo);
    },
    // 关闭显示菜单弹窗
    showMenuInfoClose() {
      this.dialogShowInfoVisible = false;
    }
  },
  components: {
    QueryOptions
  }
}
</script>

<style scoped>
.nav {
  padding-bottom: 20px;
  border-bottom: 1px solid rgb(222, 226, 230);
}

.title {
  padding: 10px;
  font-size: 14px;
  border-bottom: 1px solid rgb(222, 226, 230);
}

.tree {
  padding-bottom: 20px;
}

.top {
  display: flex;
  flex-direction: row;
}

.statu {
  display: flex;
  flex-direction: row;
}

.status {
  float: left;
  background: 1px solid rgb(0, 150, 136);
  padding: 1px;
  font-size: 14px;
  font-weight: bold;
  color: rgb(0, 150, 136);
}

.search {
  text-align: center;
}

.top-button {
  float: right;
  display: flex;
  flex-direction: row;
}

.dialog-info {
  display: flex;
  flex-direction: column;
  text-align: center;
}
</style>